package com.example.haojiutao.service.impl;


import com.example.haojiutao.common.Result;
import com.example.haojiutao.mapper.GoodsMapper;
import com.example.haojiutao.model.domain.Goods;
import com.example.haojiutao.service.GoodsService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @Description: 商品业务层实现类
 * @Author: ma_lihong
 * @CreateTime： 2024/1/23 16:53
 */
@Slf4j
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    /*
     * @Description:出售闲置
     * @Author: ma_lihong
     * @CreateTime: 2024/1/23 19:38
     * @param: goods
     * @return: com.example.haojiutao.common.Result
     */
    @Override
    public Result insertGoods(Goods goods) {
        String goodsName = goods.getGoodsName();
        String goodsType = goods.getGoodsType();
        String details = goods.getDetails();
        String sellerName = goods.getSellerName();
        Double price = goods.getPrice();
        String brand = goods.getBrand();
        String quality = goods.getQuality();
        Date listingTime = goods.getListTime();
        String picture = goods.getPicture();
        Date updateTime = goods.getUpdateTime();
        if (!StringUtils.hasLength(goodsName) || !StringUtils.hasLength(goodsType)
                || !StringUtils.hasLength(String.valueOf(price))) {
            log.warn("重要商品信息不能为空，goodsName = {},goodsType = {},price = {} ",
                    goodsName,goodsType,price);
            return Result.faile(100001,"商品名，商品类型，价格不能为空");
        }
        log.info("上架成功");
        goods.setListTime(new Date());
        int rows = goodsMapper.insertGoods(goods);
        if(rows == 1){
            return Result.success("上架成功！");
        }
        return Result.faile("上架失败");
    }

    @Override
    public Result addGoods(Goods goods) {
        String goodsName = goods.getGoodsName();
        Double price = goods.getPrice();
        if (!StringUtils.hasLength(goodsName) ||!StringUtils.hasLength(String.valueOf(price))) {
            log.warn("重要商品信息不能为空，goodsName = {}price = {} ",
                    goodsName,price);
            return Result.faile(100001,"商品名，价格不能为空");
        }

        int rows = goodsMapper.addGoods(goods);
        if(rows == 1){
            return Result.success("上架成功！");
        }
        return Result.faile("上架失败");
    }


    /*
     * @Description:更新商品价格，图片，详情，品牌
     * @Author: ma_lihong
     * @CreateTime: 2024/1/23 19:39
     * @param: goods
     * @return: com.example.haojiutao.common.Result
     */
    @Override
    public Result updateGoods(Goods goods) {
        Double price = goods.getPrice();
        if (!StringUtils.hasLength(String.valueOf(price)) ) {
            //用户校验信息不通过
            log.error("价格不能为空,gId = {}",
                    goods.getGoodsId());
            //return 0;
            return Result.faile(100002,"价钱不能为空");
        }
        goods.setUpdateTime(new Date());
        int rows = goodsMapper.updateGoods(goods);
        if(rows == 1){
            return Result.success("修改成功！");

        }
        return Result.faile("修改失败");
    }
    /*
     * @Description:下架商品,删除商品
     * @Author: ma_lihong
     * @CreateTime: 2024/1/24 20:47
     * @param: goods
     * @return: com.example.haojiutao.common.Result
     */
    @Override
    public Result deleteById(int goodsId) {
        Goods goods = new Goods();
        goods.setGoodsId(goodsId);
        goods.setDelFlag(1);
        goods.setUpdateTime(new Date());
        int rows = goodsMapper.deleteGoods(goods);
        if(rows == 1){
            return Result.success("删除成功！");
        }
        return Result.faile("删除失败");
    }
    /*
     * @Description:根据商品名称搜索商品
     * @Author: ma_lihong
     * @CreateTime: 2024/1/25 21:04
     * @return: com.example.haojiutao.common.Result
     */
    @Override
    public List<Goods> selectByName(Goods goods) {

        PageHelper.startPage(goods.getPageNum(),goods.getPageSize());
        List<Goods> goods1 = goodsMapper.selectByName(goods);
        return goods1;
    }
    /*
     * @Description: 查看单个物品，根据id查询
     * @Author: ma_lihong
     * @CreateTime: 2024/1/26 14:21
     * @param: goods
     * @return: com.example.haojiutao.common.Result
     */
    @Override
    public Goods selectById(int goodsId) {
        Goods goods = goodsMapper.selectById(goodsId);
        log.info("goods={}" + goods);

        return goods;
    }
    /*
     * @Description: 查看我卖出的
     * @Author: ma_lihong
     * @CreateTime: 2024/1/25 16:12
     * @param: goods
     * @return: java.util.List<com.example.haojiutao.model.domain.Goods>
     **/
    @Override
    public List<Goods> getRecommendbuy(Goods goods) {
        PageHelper.startPage(goods.getPageNum(),goods.getPageSize());
        return goodsMapper.getRecommendbuy(goods);
    }

    /*
     * @Description: 查看我发布的
     * @Author: ma_lihong
     * @CreateTime: 2024/1/28 16:13
     * @param: goods
     * @return: com.example.haojiutao.model.domain.Goods
     **/
    @Override
    public List<Goods> getRecommendFabu(Goods goods) {
        PageHelper.startPage(goods.getPageNum(),goods.getPageSize());
        return goodsMapper.getRecommendFabu(goods);
    }

    @Override
    public Goods getGoodsByUserId(Goods goods) {
        return goodsMapper.getGoodsByUserId(goods);
    }

    @Override
    public Result listGoods(Integer goodsId) {
        Goods goods = new Goods();
        goods.setGoodsId(goodsId);
        goods.setListFlag(0);
        goods.setListTime(new Date());
        int rows = goodsMapper.listGoods(goods);
        if(rows == 1){
            return Result.success("上架成功！");
        }
        return Result.faile("上架失败");
    }


    @Override
    public Result delistById(Integer goodsId) {
        Goods goods = new Goods();
        goods.setGoodsId(goodsId);
        goods.setListFlag(1);
        goods.setDelistingTime(new Date());
        int rows = goodsMapper.relistGoods(goods);
        if(rows == 1){
            return Result.success("删除成功！");
        }
        return Result.faile("删除失败");
    }


}
